#include <memory>
#include "Module.h"
#include "Function.h"
#include "Runtime.h"
#include "Literal.h"
#include "Identifier.h"

using namespace moses;

Module::Module(ItemList<Function> *pFuncs)
: m_pFuncs(pFuncs)
{
}

Function* Module::GetFunction(const std::string &identifier)
{
	for (size_t i = 0; i < m_pFuncs->size(); i++)
	{
		Function *pFunc = (*m_pFuncs)[i];
		if (pFunc->GetIdentifier()->GetString() == identifier)
			return pFunc; 
	}
	
	return 0;
}

Value Module::Bootstrap(Runtime *pEnv, const std::string &identifier, const std::string args)
{
	
	Literal *t = new Literal(Value(args));
	std::auto_ptr<Literal> pArgLiteral = std::auto_ptr<Literal>(t);
	
	ItemList<Expr> argList;			
	argList.push_back(t);
	
	Function *pFunc = GetFunction(identifier);
	
	if (pFunc)
		return pFunc->Call(pEnv, &argList);			
	
	return Value(__FILE__ ": No function main found");
}

ItemList<Function>* Module::GetFunctions()
{
	return m_pFuncs;
}
		
